<div class="tableform" id="dailog-batch-finance">
<form id="dailog-batch-finance-form">
    <div class="division" id="custom_html">
        <table cellpadding="0" cellspacing="0" border="0">
            <tbody>
                <tr>
                    <th>时间范围：</th>
                    <td>
                        <{input type="time" name="start_time" id="start_time" vtype="required" /}>
                    </td>
                </tr>
                <tr>
                    <th> 至： </th>
                    <td>
                        <{input type="time" name="end_time" id="end_time" vtype="required" /}>
                    </td>
                </tr>
                <tr>
                    <th> 支付宝订单号： </th>
                    <td>
                        <{input type="text" name="alipay_order_no"}>
                    </td>
                </tr>
                <tr>
                    <th> 商户订单号：</th>
                    <td>
                        <{input type="text" name="merchant_order_no"}>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    <div class="division">
        <table cellpadding="0" cellspacing="0" border="0">
            <tbody>
                <tr>
                    <th>店铺：</th>
                    <td>
                            <select id="shop_id" name="shop_id">
                            <{foreach from=$shopList item=item}>
                                <option value="<{$item.shop_id}>"><{$item.name}></option>
                            <{/foreach}>
                            </select>     
                    </td>
                </tr>
		<tr>
                <th><{t}>进度：<{/t}></th>
                    <td colspan='1'>
                    <div style="border:1px solid #CCC;width:300px;overflow:hidden;float:left;background:#FFF;padding:1px;">
                        <div id="processBar" style="background:#7493D1;width:1%;height:15px;">&nbsp;</div>
                    </div>&nbsp;<span id="processPercent">0%</span>
                     </td>
                </tr>
            </tbody>
        </table>               
    </div>
    
</form>
</div>
<{area inject=".mainFoot"}>
    <div class="table-action">
        <{button id="down1" type="button" label="开始" class="btn-primary"}>
	<{button id="down2" class="btn1-primary" label=$___b2c="下载中"|t:'b2c' type="button" style="display:none;"}>
        <{button id="return-btn" type="button" label="关闭" class="btn-secondary" isCloseDialogBtn="true"}>
    </div>
<{/area}>

<script type="text/javascript">

    $('down1').addEvent('click',function(){
        var start_time = $E('input[name=start_time]').get('value');
        var end_time = $E('input[name=end_time]').get('value');

        uploadToTaobao(start_time,end_time,1);

 });
   
   function setProcessBar(id,w){
        document.getElementById(id).style.width = w;
    }

   function uploadToTaobao(start_time,end_time,page_no){
        if(!page_no) {page_no=1;}
	 var sta_str = new Date(start_time);
         var end_date = new Date(end_time);
	 document.getElementById('return-btn').style.display="none";
         document.getElementById('down1').style.display="none";
         document.getElementById('down2').style.display="inline";

        if(sta_str>end_date){
            alert('开始时间必须小于结束时间');
            return false;
        }
        if(!start_time||!end_time){
            alert('请选择开始时间或结束时间');
            return false;
        }

        var num = (end_date-sta_str)/(1000*3600*24);
        var days = parseInt(Math.ceil(num));
        if(days>7){
            alert('结束时间与开始时间,必须间隔在七天之内 ');
            return false;
        }
        var alipay_order_no = $E('input[name=alipay_order_no]').get('value');
        var merchant_order_no =$E('input[name=merchant_order_no]').get('value');
	 var shop_id =$E('select[name=shop_id]').get('value');
	new Request({
            url: 'index.php?app=finance&ctl=bill&act=do_trade_search',
            data:'start_time='+start_time+'&shop_id='+shop_id+'&end_time='+end_time+'&page_no='+page_no+'&alipay_order_no='+alipay_order_no+'&merchant_order_no='+merchant_order_no,
	    method:'post',
	    onSuccess:function(responseText){
		 var cd = responseText.split('@');
		 setProcessBar('processBar',Math.round(cd[1])+'%');
		 document.getElementById('processPercent').innerHTML = Math.round(cd[1])+'%';
	      if (cd[0] == 'success') 
                {
		     page_no++;
                    uploadToTaobao(start_time,end_time,page_no);

                }else if(cd[0] == 'finish'){
                       setProcessBar('processBar','100%');
		       document.getElementById('processPercent').innerHTML = '100%';
                	 document.getElementById('return-btn').style.display="inline";
                        document.getElementById('down1').style.display="inline";
                        document.getElementById('down2').style.display="none";
		}else{
			document.getElementById('processPercent').innerHTML = '失败';
                    document.getElementById('return-btn').style.display="inline";
                }
            }
        }).send();
    };


</script>
